<?php
/*
	本插件：$this->model('agent')->方法名();
	其他地方调用：model('jy_aiagent/agent')->方法名();
*/
namespace jy_aiagent;
defined('BY_JYA') or exit('error');
class agent extends \plugin{
	public function getdemo(){
		$item=pdo_get('jy_aiagent_agent',array('isdemo'=>1,'uniacid'=>$_SESSION['uniacid']));
		if(!$item){
			$item=pdo_get('jy_aiagent_agent',array('status'=>1,'uniacid'=>$_SESSION['uniacid']));
		}
		return $item;
	}
	public function checkagent($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$mid){
			return '';
		}
		$isadd=pdo_get('jy_aiagent_useragent',array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isadd['id']){
			return '';
		}
		$item=$this->getdemo();
		if(!$item){
			return '';
		}
		$data=array(
			'title'=>trim($item['title']),
			'logo'=>trim($item['logo']),
			'desc'=>trim($item['desc']),
			'tts_voices'=>$item['tts_voices'],
			'default_tts_voice'=>$item['default_tts_voice'],
			'voiceid'=>$item['voiceid'],
			'llm_model'=>trim($item['llm_model']),
			'ainame'=>trim($item['ainame']),
			'user_name'=>trim($item['user_name']),
			'groupid'=>intval($item['groupid']),
			'status'=>1,
			'tts_speech_speed'=>trim($item['tts_speech_speed']),
			'asr_speed'=>trim($item['asr_speed']),
			'tts_pitch'=>trim($item['tts_pitch']),
			'tts_voice_name'=>trim($item['tts_voice_name']),
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aiagent_useragent',$data);
		return $data;
	}
	public function addgoodsagent($mid=0,$item=array()){
		$mid=$mid?:$_SESSION['uid'];
		if(!$mid){
			return '';
		}
		$item=$item?:$this->getdemo();
		if(!$item){
			return '';
		}
		$data=array(
			'title'=>trim($item['title']),
			'logo'=>trim($item['logo']),
			'desc'=>trim($item['desc']),
			'tts_voices'=>$item['tts_voices'],
			'default_tts_voice'=>$item['default_tts_voice'],
			'voiceid'=>$item['voiceid'],
			'llm_model'=>trim($item['llm_model']),
			'ainame'=>trim($item['ainame']),
			'user_name'=>trim($item['user_name']),
			'groupid'=>intval($item['groupid']),
			'status'=>2,
			'tts_speech_speed'=>trim($item['tts_speech_speed']),
			'asr_speed'=>trim($item['asr_speed']),
			'tts_pitch'=>trim($item['tts_pitch']),
			'tts_voice_name'=>trim($item['tts_voice_name']),
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aiagent_useragent',$data);
		$data['id']=pdo_insertid();
		$voice=pdo_get('jy_aiagent_voice',array('id'=>$item['voiceid'],'uniacid'=>$_SESSION['uniacid']));
		$data['tts_voice']=$voice['voiceid'];
		$data['language']=$voice['language'];
		$res=$this->model('api')->adduseragents($data,0,1);
		pdo_update('jy_aiagent_useragent',array('apiid'=>trim($res['id'])),array('id'=>$data['id'],'uniacid'=>$_SESSION['uniacid']));
		$data['apiid']=trim($res['id']);
		return $data;
	}
	public function adduseragents($postdata=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$voice=pdo_get('jy_aiagent_voice',array('id'=>$postdata['voiceid'],'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'title'=>trim($postdata['title']),
			'logo'=>trim($postdata['logo']),
			'desc'=>trim($postdata['desc']),
			'default_tts_voice'=>$voice['language'].':'.$voice['voiceid'],
			'voiceid'=>$postdata['voiceid'],
			'llm_model'=>trim($postdata['llm_model']),
			'ainame'=>trim($postdata['ainame']),
			'user_name'=>trim($postdata['user_name']),
			'paixu'=>intval($postdata['paixu']),
			'tplid'=>trim($postdata['tplid']),
			'status'=>$postdata['status']?:1,
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_aiagent_useragent',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			//绑定设备的智能体才API
			if($data['status']==2){
				$data['tts_voice']=$voice['voiceid'];
				$data['language']=$voice['language'];
				$data['apiid']=$postdata['apiid'];
				$res=$this->model('api')->adduseragents($data,$id,1);
				if(!$data['apiid']){
					pdo_update('jy_aiagent_useragent',array('apiid'=>trim($res['id'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}else{
			$demo=$this->model('agent')->getdemo();
			$data['tts_voices']=$data['tts_voices']?:$demo['tts_voices'];
			$data['tts_speech_speed']=$data['tts_speech_speed']?:$demo['tts_speech_speed'];
			$data['asr_speed']=$data['asr_speed']?:$demo['asr_speed'];
			$data['tts_pitch']=$data['tts_pitch']?:$demo['tts_pitch'];
			$data['tts_voice_name']=$data['tts_voice_name']?:$demo['tts_voice_name'];
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$mid;
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_aiagent_useragent',$data);
			$data['id']=pdo_insertid();
			$id=$data['id'];
			if($data['status']==2){
				$data['tts_voice']=$voice['voiceid'];
				$data['language']=$voice['language'];
				$res=$this->model('api')->adduseragents($data,0,1);
				pdo_update('jy_aiagent_useragent',array('apiid'=>trim($res['id'])),array('id'=>$data['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return $id;
	}
	public function peigoodscode($post=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$post['code']){
			exi('请输入设备播报的6位数验证码','error');
		}
		$data=array(
			'code'=>trim($post['code']),
			'agentid'=>trim($post['agentid']),
			'status'=>1,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		$agentapiid=pdo_getcolumn('jy_aiagent_useragent',array('id'=>$post['agentid'],'uniacid'=>$_SESSION['uniacid']),'apiid');
		$res=$this->model('api')->addusergoods($data,$agentapiid,1);
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($res));
		$data['macsn']=trim($res['macAddress']);
		$data['deviceid']=trim($res['id']);
		$data['title']=$data['alias']=trim($res['board_name']);
		$data['gu_v']=trim($res['app_version']);
		$data['agentapiid']=trim($agentapiid);
		if($post['goodslogid']){
			$isadd=pdo_get('jy_aiagent_goodslog',array('id'=>$post['goodslogid'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$isnum=pdo_count('jy_aiagent_goodslog',array('agentid'=>$post['agentid'],'mid'=>$mid,'deviceid'=>0,'uniacid'=>$_SESSION['uniacid']));
			if($isnum==1){
				$isadd=pdo_get('jy_aiagent_goodslog',array('agentid'=>$post['agentid'],'mid'=>$mid,'deviceid'=>0,'uniacid'=>$_SESSION['uniacid']));
			}else{
				$isadd=pdo_get('jy_aiagent_goodslog',array('macsn'=>trim($res['macAddress']),'deviceid'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		if($isadd['id']){
			$update=array(
				'alias'=>trim($res['board_name']),
				'code'=>trim($post['code']),
				'deviceid'=>trim($res['id']),
				'gu_v'=>trim($res['app_version']),
			);
			pdo_update('jy_aiagent_goodslog',$update,array('id'=>$isadd['id'],'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			pdo_insert('jy_aiagent_goodslog',$data);
		}
		return $data;
	}
	public function addshengwen($post=array(),$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
			'title'=>trim($post['title']),
			'desc'=>trim($post['desc']),
			'mp3'=>trim($post['mp3']),
			'embedding'=>trim($post['embedding']),
		);
		$agentapiid=pdo_getcolumn('jy_aiagent_useragent',array('id'=>$post['agentid'],'uniacid'=>$_SESSION['uniacid']),'apiid');
		if($post['id']){
			$res=$this->model('api')->editshengwen($data,$agentapiid,1,$post['apiid']);
		}else{
			$res=$this->model('api')->addshengwen($data,$agentapiid,1);
		}
		//file_put_contents(ROOT_D.'/1.txt',ijson_encode($res));
		$data['value']=trim($res['value']);
		if($post['id']){
			pdo_update('jy_aiagent_shengwenlog',$data,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$_SESSION['uid'];
			$data['apiid']=$res['id'];
			$data['agentid']=$post['agentid'];
			$data['agentapiid']=trim($agentapiid);
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_aiagent_shengwenlog',$data);
		}
		return '';
	}
	public function getchat($agent=array(),$exi=0){
		//$agent['apiid']=559956;
		$list=$this->model('api')->getchat($agent['apiid'],$exi);
		pdo_update('jy_aiagent_useragent',array('getchattime'=>time()),array('id'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
		if(!count($list)){
			if($exi){
				exi('没有任何历史对话！','error');
			}else{
				return '';
			}
		}
		$dateObj=new \DateTime();
		foreach($list as $item){
			//file_put_contents(ROOT_D.'/chat.txt',ijson_encode($item));
			$title = mb_convert_encoding($item['chat_summary']['title'], 'UTF-8', 'auto');
			$summary = mb_convert_encoding($item['chat_summary']['summary'], 'UTF-8', 'auto');
			$data=array(
				'chat_id'=>$item['chat_id'],
				'title'=>base64_encode($title),
				'summary'=>base64_encode($summary),
				'agentid'=>trim($agent['id']),
				'mid'=>trim($agent['mid']),
				'userid'=>$item['user_id'],
				'device_id'=>$item['device_id'],
				'mac_address'=>$item['mac_address'],
				'msg_count'=>$item['msg_count'],
				'agentidapi'=>$item['agent_id'],
				'chatapiid'=>$item['id'],
				'model'=>$item['model'],
				'token_count'=>$item['token_count'],
				'duration'=>$item['duration'],
				'status'=>1,
			);
			$isadd=pdo_get('jy_aiagent_useragentlog',array('chatapiid'=>$item['id'],'agentid'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
			$id=intval($isadd['id']);
			if($id){
			}else{
				$data['uniacid']=$_SESSION['uniacid'];
				$dateObj->modify($item['created_at']);
				$data['createtime']=$dateObj->getTimestamp(); 
				pdo_insert('jy_aiagent_useragentlog',$data);
				$chatid=pdo_insertid();
				$messages=$this->model('api')->getchatitem($agent['apiid'],$item['id'],$exi);
				//file_put_contents(ROOT_D.'/messages.txt',ijson_encode($messages));
				foreach($messages as $sv){
					$content = mb_convert_encoding($sv['content'], 'UTF-8', 'auto');
					$data=array(
						'name'=>trim($sv['name']),
						'content'=>base64_encode($content),
						'agentid'=>trim($agent['id']),
						'mid'=>trim($agent['mid']),
						'userid'=>$item['user_id'],
						'chatid'=>$chatid,
						'chatapiid'=>$item['id'],
						'role'=>$sv['role'],
						'embeddingid'=>$sv['voice_embedding_id'],
						'embedding'=>$sv['embedding']?ijson_encode($sv['embedding']):'',
						'mp3'=>$sv['url'],
					);
					$data['uniacid']=$_SESSION['uniacid'];
					$dateObj->modify($sv['created_at']);
					$data['createtime']=$dateObj->getTimestamp(); 
					pdo_insert('jy_aiagent_useragentlogitem',$data);
				}
			}
		}
	}
	public function getembedding($agent=array(),$exi=0){
		//$agent['apiid']=559956;
		$list=$this->model('api')->getembedding($agent['apiid'],$exi);
		//file_put_contents(ROOT_D.'/getembedding.txt',ijson_encode($list));
		$dateObj=new \DateTime();
		foreach($list as $item){
			$content = mb_convert_encoding($item['content'], 'UTF-8', 'auto');
			$isadd=pdo_get('jy_aiagent_useragentlogitem',array('mp3'=>$item['url'],'agentid'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
			$id=intval($isadd['id']);
			if($id && !$item['embedding']){
				pdo_update('jy_aiagent_useragentlogitem',array('embedding'=>ijson_encode($item['embedding'])),array('id'=>$id,'agentid'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				return '';
				$chat=pdo_get('jy_aiagent_useragentlog',array('chatapiid'=>$item['chat_id'],'agentid'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
				$data=array(
						'name'=>trim($item['name']),
						'content'=>base64_encode($content),
						'agentid'=>trim($agent['id']),
						'mid'=>trim($agent['mid']),
						'userid'=>$item['user_id'],
						'chatid'=>$chat['id'],
						'role'=>$item['role'],
						'embeddingid'=>$item['voice_embedding_id'],
						'embedding'=>ijson_encode($item['embedding']),
						'mp3'=>$item['url'],
				);
				$data['uniacid']=$_SESSION['uniacid'];
				$dateObj->modify($item['created_at']);
				$data['createtime']=$dateObj->getTimestamp(); 
				pdo_insert('jy_aiagent_useragentlogitem',$data);
			}
		}
		return '';
	}
}
?>